package cn.icanci.controller.superadmin;

import cn.icanci.entity.bo.HeadLine;
import cn.icanci.entity.dto.Result;
import cn.icanci.service.solo.HeadLineService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.simpleframework.core.annotation.Controller;
import org.simpleframework.inject.annotation.Autowired;
import org.simpleframework.mvc.annotation.RequestMapping;
import org.simpleframework.mvc.annotation.RequestParam;
import org.simpleframework.mvc.annotation.ResponseBody;
import org.simpleframework.mvc.type.ModelAndView;
import org.simpleframework.mvc.type.RequestMethod;

/**
 * 头条Controller
 *
 * @author icanci
 * @date Created in 2021/02/04 00:12
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/headline")
public class HeadLineOperationController {
    /**
     * TODO 这里的根据名字映射出现问题
     *  @Autowired(value = "headLineServiceImpl")
     */
    @Autowired
    private HeadLineService headLineService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addHeadLine(@RequestParam("lineName") String lineName, @RequestParam("lineLink") String lineLink, @RequestParam("lineImg") String lineImg,
                                    @RequestParam("priority") Integer priority) {
        HeadLine headLine = new HeadLine();
        headLine.setLineName(lineName);
        headLine.setLineLink(lineLink);
        headLine.setLineImg(lineImg);
        headLine.setPriority(priority);
        Result<Boolean> booleanResult = headLineService.addHeadLine(headLine);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView("addheadline.jsp").addViewData("result", booleanResult);
        return modelAndView;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    public void removeHeadLineById() {
        // TODO:参数检验和请求参数转换
        System.out.println("head line");
        //        return headLineService.removeHeadLineById(1);
    }

    public Result<Boolean> modifyHeadLine(HttpServletRequest req, HttpServletResponse resp) {
        // TODO:参数检验和请求参数转换
        return headLineService.modifyHeadLine(new HeadLine());
    }

    public Result<HeadLine> queryHeadLineById(HttpServletRequest req, HttpServletResponse resp) {
        // TODO:参数检验和请求参数转换
        return headLineService.queryHeadLineById(1);
    }

    @RequestMapping(value = "query", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<HeadLine>> queryHeadLine(@RequestParam("req") HttpServletRequest req, @RequestParam("resp") HttpServletResponse resp) {
        return headLineService.queryHeadLine(new HeadLine(), 1, 100);
    }
}
